#!/bin/bash
#pNote
#Copyright 2010,2011,2014,2015
#Sigmund Berglund - Valåmoen, Norway.

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------

BG=$1; [ ! "$BG" ] && BG='#FFF4AF'
FG=$2; [ ! "$FG" ] && FG='#222222'
VERSION=0.3

set -a

check_exit (){
	#because of focus-trouble in jwm-tray, this workaround is required. might be jwm-version specific
	if [ `getcurpos | cut -d' ' -f1` -gt 250 ] || [ `getcurpos | cut -d' ' -f2` -gt $HEIGHT ]; then
		echo -n "$PAD" > $HOME/.pnote
		for I in `busybox ps | grep -wF pNote | awk '{print $1}'`; do kill -9 $I; done
		exit
	fi
}

#set colored background
echo 'style "menu" {
  font_name				= "DejaVu sans italic 12"
  bg[NORMAL]			= "'$BG'"
  base[NORMAL]			= "'$BG'"
  text[NORMAL]			= "'$FG'"
  }
class "*" style "menu"' > /tmp/gtkrc
export GTK2_RC_FILES=/tmp/gtkrc

#find height of screen
TMP=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
HEIGHT=$((TMP-27)) #minus main-tray
[ ! "$HEIGHT" ] && HEIGHT=571 #in case

#run app
export pNote='
<window title="pNote" width-request="250" height-request="'$HEIGHT'" decorated="false">
  <edit left-margin="5" wrap-mode="2"><variable>PAD</variable><input file>'$HOME'/.pnote</input></edit> 
  <action signal="leave-notify-event">echo "$PAD" | xclip -i -selection clipboard</action> 
  <action signal="leave-notify-event">check_exit</action>
</window>'
gtkdialog -p pNote -G +0+0


